/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

//using _videoConference.BAL;
using System.Data;
using System.Data.SqlClient;
using _videoConference.BAL;

namespace _videoConference
{
    public partial class AddPageControl : System.Windows.Controls.UserControl,IDisposable
    {
        DataSet ds = new DataSet();
        string s;
        IAddPage ap = new IAddPage();
        private bool disposed = false;

        public AddPageControl()
        {
           InitializeComponent();          
           s = cmbFileLoc + "\\" + cmbFileName;          
        }       

        public void update(object o, EventArgs e)
        {
            ds = ap.getpagename();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (txtPageName.Text == ds.Tables[0].Rows[i][0].ToString())
                {
                    MessageBox.Show("The page name entered already exist. Enter another name!!");                 
                    txtPageName.Text = "";
                    return;
                }

                if (txtPageName.Text == "" || txtPageName.Text == null)
                {
                    MessageBox.Show("Enter page name!!");
                    txtPageName.Text = "";
                    return;
                }
            }
           
            if (cmbMenu.SelectedItem != null)
            {
                TextRange tr = new TextRange(txtPageDesc.Document.ContentStart, txtPageDesc.Document.ContentEnd);                
                ap.dbpage(txtPageName.Text, txttitle.Text, tr.Text, ((MyMenu123)((MenuItem)((ComboBoxItem)cmbMenu.SelectedItem).Tag).Tag).MenuId.ToString());
                ap.deletemenu();
                             
                if (((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count > 0)
                {
                    for (int i = 0; i < ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count; i++)
                    {
                        RemoveN((MenuItem)((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items[i], true);
                    }
                }
                ((MyMenu123)((MenuItem)((ComboBoxItem)cmbMenu.SelectedItem).Tag).Tag).pagename = txtPageName.Text;
                if (((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count > 0)
                {
                    for (int i = 0; i < ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count; i++)
                    {
                        WriteN((MenuItem)((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items[i], "");
                    }
                }
                ap.updtmenutable(((MyMenu123)((MenuItem)((ComboBoxItem)cmbMenu.SelectedItem).Tag).Tag).MenuId, txtPageName.Text);
     
                ((Button)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("btnDelete")).IsEnabled = true;
                ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvM3")).IsEnabled = true;
                ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvM2")).IsEnabled = true;
                ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvM4")).IsEnabled = true;
                ((Button)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("btnAdd")).IsEnabled = true;
                ((Button)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("btnCreate")).IsEnabled = true;
                ((RowDefinition)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("rd1")).Height = GridLength.Auto;
                GenerateModule gm = new GenerateModule();
                gm.pagename = txtPageName.Text;
                ((InkCanvas)this.Parent).Children.Add(gm);
                cmbMenu.Items.Clear();
                txtPageName.Text = "";
                txttitle.Text = "";
                cmbMenu.SelectedItem = null;
                txtRootMenu.Text = "";
                txtMenuText.Text = "";
                txtPageDesc.SelectAll();
                txtPageDesc.Selection.Text = "";                
                ((InkCanvas)this.Parent).Children.Remove(this);               
            }
        }

        public void refresh(object o, EventArgs e)
        {
            txtPageName.Text = "";
            txttitle.Text = "";
            cmbMenu.SelectedItem = null;
            txtRootMenu.Text = "";
            txtMenuText.Text = "";          
            txtPageDesc.SelectAll();
            txtPageDesc.Selection.Text = "";
        }

        public void cancel(object sender, EventArgs e)
        {
            txtPageName.Text = "";
            txttitle.Text = "";
            cmbMenu.SelectedItem = null;
            txtRootMenu.Text = "";
            txtMenuText.Text = "";
            txtPageDesc.SelectAll();
            txtPageDesc.Selection.Text = "";

            if (((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count > 0)
            {
                for (int i = 0; i < ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count; i++)
                {

                    RemoveN((MenuItem)((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items[i], true);

                }
            }
            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
            ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = false; 
            ((InkCanvas)this.Parent).Children.Remove(this);           
        }

        public void RemoveN(MenuItem curr, bool rot)
        {
            if (rot)
            {
                if (curr.Header.ToString() == "*NEW ITEM*")
                {
                    ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Remove(curr);

                }
                else
                {
                    if (curr.Items.Count > 0)
                    {
                      //  curr.MouseDoubleClick -= new MouseButtonEventHandler(mnuitem_MouseDoubleClick);

                        for (int i = 0; i < curr.Items.Count; i++)
                        {
                            RemoveN((MenuItem)curr.Items[i], false);
                        }
                    }
                }
            }
            else
            {
                if (curr.Header.ToString() == "*NEW ITEM*")
                {
                    ((MenuItem)curr.Parent).Items.Remove(curr);

                }
                else
                {
                    if (curr.Items.Count > 0)
                    {
                      //  curr.MouseDoubleClick -= new MouseButtonEventHandler(mnuitem_MouseDoubleClick);
                        for (int i = 0; i < curr.Items.Count; i++)
                        {
                            RemoveN((MenuItem)curr.Items[i], false);
                        }
                    }
                }
            }
        }

        public void WriteN(MenuItem curr, string rot)
        {
          
            int id=Int32.Parse(((MyMenu123)(curr).Tag).MenuId.ToString());
            int parid = Int32.Parse(((MyMenu123)(curr).Tag).ParentId.ToString());
            string pagename;
            if (curr.Equals(((MenuItem)((ComboBoxItem)cmbMenu.SelectedItem).Tag)))
            {
                pagename = ((MyMenu123)((MenuItem)((ComboBoxItem)cmbMenu.SelectedItem).Tag).Tag).pagename.ToString();
            }
            else
            {
                pagename = ((MyMenu123)(curr).Tag).pagename.ToString();
            }

            byte[] myBytes = null;
            byte[] b = null;
            if (((MyMenu123)curr.Tag).btmImg != null)
            {
                myBytes = new byte[int.Parse(((MyMenu123)(curr).Tag).btmImg.StreamSource.Length.ToString())];
                ((MyMenu123)curr.Tag).btmImg.StreamSource.Position = 0;
                ((MyMenu123)curr.Tag).btmImg.StreamSource.Read(myBytes, 0, int.Parse(((MyMenu123)curr.Tag).btmImg.StreamSource.Length.ToString()));
                b = myBytes;
            }
          
            
            string capstr = ((MyMenu123)(curr).Tag).Caption.ToString();
  
            try
            {
                DataSet ds1 = ap.savemenuitem(id, parid, b, capstr,pagename);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--AddModule.xaml.cs--:--WriteN()--:----:--");
                clsUtility.LogException = exp;
                //MessageBox.Show("1videoConference--:--AddModule.xaml.cs--:--WriteN()--:--" + exp.Message);
            }
            if (curr.Items.Count > 0)
            {
                for (int i = 0; i < curr.Items.Count; i++)
                {
                    WriteN((MenuItem)curr.Items[i], rot + "--");
                }
            }
            else
            {
                
            }
        }

        public void fillcombo()
        {
            cmbMenu.Items.Clear();
            if (((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count > 0)
            {
                for (int i = 0; i < ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items.Count; i++)
                {
                    WriteN1((MenuItem)((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).Items[i], "");
                }
            }
        }

        public void WriteN1(MenuItem curr, string rot)
        {
            if (curr.Items.Count > 1)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = rot + curr.Header;
                cbi.Tag = curr;
                cbi.Background = Brushes.Black;
                cbi.Foreground = System.Windows.Media.Brushes.Red;
                cbi.IsEnabled = false;
                cmbMenu.Items.Add(cbi);

                for (int i = 0; i < curr.Items.Count; i++)
                {
                    WriteN1((MenuItem)curr.Items[i], rot + "--");
                }
            }
            //else if (curr.Header.ToString() != "*NEW ITEM*")
            else if(curr.Items.Count == 1)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = rot + curr.Header;
                cbi.Tag = curr;
                cbi.IsEnabled = true;
                cbi.Background = Brushes.Black;
                cbi.Foreground = Brushes.White;
                cmbMenu.Items.Add(cbi);
            }
            
        }

         public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);    
        }

         private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                { 
                   
                }        
                disposed = true;

            }
        }

        ~AddPageControl()
        {
            Dispose(false);
        }



    }
}